package com.xpchenfrank.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Json工具类
 * 
 * @time 2017年4月24日
 * @author CiCi
 * @copyright Wuxi Yazuo ,Ltd.copyright 2015-2025
 *
 */
@Slf4j
public class JsonUtils extends ObjectMapper {

    private static final long serialVersionUID = 4327047514081803096L;

    public JsonUtils() {
        // 设置可用单引号
        this.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        // 设置字段可以不用双引号包括
        this.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        // 设置实体无属性和json串属性对应时不会出错
        this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        this.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        this.registerModule(javaTimeModule);
    }

    public static <T> String toJson(T t) {
        if(t == null){
            return null;
        }
        try {
            JsonUtils mapper = new JsonUtils();
            log.debug("Switching Class[{}] to JSON Text.", t.getClass().getName());
            SimpleFilterProvider filterProvider = new SimpleFilterProvider()
                    .addFilter("standardEngineValve", SimpleBeanPropertyFilter.filterOutAllExcept("container"))
                    .addFilter("servletWebRequest", SimpleBeanPropertyFilter.filterOutAllExcept("request"));
                    mapper.setFilterProvider(filterProvider);
            return mapper.writeValueAsString(t);
        } catch (JsonProcessingException e) {
            // 容错去掉抛出的异常，直接返回空值，由调用方判断结果
            return null;
        }
    }

    public static <T> T fromJson(String json, Class<T> clazz) {
        try {
            return new JsonUtils().readValue(json, clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("rawtypes")
    public static List fromJsonToList(String json, Class<?>... elementClasses) {
        try {
            JsonUtils jsonUtils = new JsonUtils();

            JavaType javaType = jsonUtils.getTypeFactory().constructParametricType(ArrayList.class, elementClasses);
            ArrayList lst =  jsonUtils.readValue(json, javaType);
            return lst;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}